package com.hqyj.system.controller;

import com.hqyj.system.model.SysUser;
import com.hqyj.utils.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("sysUserController")
public class SysUserController extends BaseController {
    //1.列表
    @RequestMapping("/list")
    public String list(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysUser sysUser) throws IOException {
        System.out.println("------SysUserControl------list()--------");
        List<SysUser> sysUserList_db =sysUserService.queryAllSysUser();
        model.addAttribute("sysUserList_db", sysUserList_db);
        return "view/system/sysUser/sysUserList";
    }
    /**
     * 2.增加
     *
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysUser
     * @return
     * @throws IOException
     */
    @RequestMapping("/add")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysUser sysUser) throws IOException {
        //1.接收数据
//        System.err.println("------SysUserController-----------add()-----------------" + sysUser);
        //3.调用业务
        int n = sysUserService.addSysUserBySysUser(sysUser);

        //2.跳转页面
        //返回数据
        List<SysUser> sysUserList_db = sysUserService.queryAllSysUser();
        model.addAttribute("sysUserList_db", sysUserList_db);
        return "view/system/sysUser/sysUserList";
    }

    //3.删除
    @RequestMapping("/delete")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysUser sysUser, String userIdStr) throws IOException {
        //1.接收数据
//        System.err.println("------SysUserController-----------delete()-----------------" + userIdStr);
        //3.调用业务
        //字符类型转数字类型
        int userId = Integer.parseInt(userIdStr);
        int n = sysUserService.deleteSysUserByUserId(userId);

        //2.跳转页面
        //返回数据
        List<SysUser> sysUserList_db = sysUserService.queryAllSysUser();
        model.addAttribute("sysUserList_db", sysUserList_db);
        return "view/system/sysUser/sysUserList";
    }


    //4.1修改UI
    @RequestMapping("/updateUI")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysUser sysUser, String userIdStr) throws IOException {
        //1.接收数据
//        System.err.println("------SysUserController-----------updateUI()-----------------" + userIdStr);
        //3.调用业务
        //字符类型转数字类型
        int userId = Integer.parseInt(userIdStr);
        SysUser sysUser_db = sysUserService.querySysUserByUserId(userId);
        model.addAttribute("sysUser_db", sysUser_db);

        //2.跳转页面
        return "view/system/sysUser/updateSysUser";
    }


    //4.2修改
    @RequestMapping("/update")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysUser sysUser, String userIdStr) throws IOException {
        //1.接收数据
        int userId = Integer.parseInt(userIdStr);
        sysUser.setUserId(userId);
        //3.调用业务
        int n = sysUserService.updateSysUserBySysUser(sysUser);

        //2.跳转页面
        //返回数据
        List<SysUser> sysUserList_db = sysUserService.queryAllSysUser();
        model.addAttribute("sysUserList_db", sysUserList_db);
        return "view/system/sysUser/sysUserList";
    }

    //5.查询
}
